package com.yuke.cloud.service.pmc.service.impl;

import com.yuke.cloud.service.api.opc.dto.UploadFile;
import com.yuke.cloud.service.api.opc.service.OpcFileFeignApi;
import com.yuke.cloud.service.pmc.dao.ProdRegionResortIntroduceMapper;
import com.yuke.cloud.service.pmc.dto.RegionResortIntroducePictureDto;
import com.yuke.cloud.service.pmc.entity.ProdRegionResortIntroduce;
import com.yuke.cloud.service.pmc.entity.ProdRegionResortPicture;
import com.yuke.cloud.service.pmc.service.ProdRegionResortIntroduceService;
import com.yuke.cloud.common.core.support.BaseService;
import com.yuke.cloud.service.pmc.service.ProdRegionResortPictureService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * Created by zys on 2018/09/30.
 */
@Service
@Transactional
public class ProdRegionResortIntroduceServiceImpl extends BaseService<ProdRegionResortIntroduce> implements ProdRegionResortIntroduceService {
    @Resource
    private ProdRegionResortIntroduceMapper prodRegionResortIntroduceMapper;
    @Resource
    private ProdRegionResortPictureService prodRegionResortPictureService;
    @Resource
    private OpcFileFeignApi opcFileFeignApi;

    @Override
    public int save(ProdRegionResortIntroduce record) {
        record.setResortIntroduceId(generateId());
        return super.save(record);
    }

    @Override
    public int update(ProdRegionResortIntroduce entity) {
        int count = super.update(entity);
        if (null != entity.getProdRegionResortPictures() || entity.getProdRegionResortPictures().size() > 0) {
            List<ProdRegionResortPicture> prodRegionResortPictures = entity.getProdRegionResortPictures();
            UploadFile uploadFile = new UploadFile();
            List<String> filePaths = new ArrayList<>();
            for (ProdRegionResortPicture prodRegionResortPicture : prodRegionResortPictures) {
                if (prodRegionResortPicture.getResortPictureId() == null) {
                    //新增
                    prodRegionResortPicture.setResortId(entity.getResortIntroduceId());
                    prodRegionResortPictureService.save(prodRegionResortPicture);
                } else{
                    if (prodRegionResortPicture.getOldBigPicUri() != null && prodRegionResortPicture.getOldSmallPicUri() != null) {
                        //删除
                        prodRegionResortPictureService.deleteById(prodRegionResortPicture.getResortPictureId());
                        if (prodRegionResortPicture.getOldSmallPicUri() != null && !"".equals(prodRegionResortPicture.getOldSmallPicUri()))
                            filePaths.add(prodRegionResortPicture.getOldSmallPicUri());
                        if (prodRegionResortPicture.getOldBigPicUri() != null && !"".equals(prodRegionResortPicture.getOldBigPicUri()))
                            filePaths.add(prodRegionResortPicture.getOldBigPicUri());
                    }

                    // add by wg 20190515 更新图片记录
                    if (prodRegionResortPicture.getOldBigPicUri() == null && prodRegionResortPicture.getOldSmallPicUri() == null) {
                        Example example = new Example(ProdRegionResortPicture.class);
                        Example.Criteria criteria = example.createCriteria();
                        criteria.andEqualTo("resortPictureId", prodRegionResortPicture.getResortPictureId());
                        prodRegionResortPictureService.updateByExample(prodRegionResortPicture, example);
                    }
                }
            }
            uploadFile.setFilePaths(filePaths);
            opcFileFeignApi.filesDelete(uploadFile);
        }
        return count;
    }

    @Override
    public List<RegionResortIntroducePictureDto> findByRecommend() {
        return prodRegionResortIntroduceMapper.findByRecommend();
    }

    @Override
    public void deleteById(Long id) {
        List<ProdRegionResortPicture> prodRegionResortPictureList = prodRegionResortPictureService.getProdRegionResortPictureByResortIntroduceId(id);
        super.deleteById(id);
        ProdRegionResortPicture prodRegionResortPicture = new ProdRegionResortPicture();
        prodRegionResortPicture.setResortId(id);
        prodRegionResortPictureService.delete(prodRegionResortPicture);
        this.deletePicFromFastDFS(prodRegionResortPictureList);
    }

    private void deletePicFromFastDFS(List<ProdRegionResortPicture> prodRegionResortPictureList) {
        UploadFile uploadFile = new UploadFile();
        List<String> filePaths = new ArrayList<>();
        for (ProdRegionResortPicture prodRegionResortPicture : prodRegionResortPictureList) {
            if (prodRegionResortPicture.getBigPicUri() != null && !"".equals(prodRegionResortPicture.getBigPicUri()))
                filePaths.add(prodRegionResortPicture.getBigPicUri());
            if (prodRegionResortPicture.getSmallPicUri() != null && !"".equals(prodRegionResortPicture.getSmallPicUri()))
                filePaths.add(prodRegionResortPicture.getSmallPicUri());
        }
        if (filePaths != null && filePaths.size() > 0) {
            uploadFile.setFilePaths(filePaths);
            opcFileFeignApi.filesDelete(uploadFile);
        }
    }

    @Override
    public void deleteByIds(String ids) {
        List<ProdRegionResortIntroduce> prodRegionResortIntroduces = prodRegionResortIntroduceMapper.selectByIds(ids);
        List<ProdRegionResortPicture> prodRegionResortPictureList = prodRegionResortPictureService.getProdRegionResortPictureByRegionIds(prodRegionResortIntroduces.stream().map(ProdRegionResortIntroduce::getRegionId).collect(Collectors.toList()));
        super.deleteByIds(ids);
        this.deletePicFromFastDFS(prodRegionResortPictureList);
    }

    @Override
    public List<ProdRegionResortIntroduce> getByCondition(ProdRegionResortIntroduce prodRegionResortIntroduce) {
        return prodRegionResortIntroduceMapper.getByCondition(prodRegionResortIntroduce);
    }

    @Override
    public void deleteByRegionIds(List<Long> regionIds) {
        prodRegionResortIntroduceMapper.deleteByRegionIds(regionIds);
    }
}
